#import "SupplyAndDemandSearchVC.h"
#import "SupplyAndDemandCell.h"
#import "FUIImageView.h"
#import "SupplyAndDemandSearchSuggestionVC.h"
@interface SupplyAndDemandSearchVC ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>{
    FUIImageView      *seachV;
}
@property(strong,nonatomic)UITableView *myTableview;
@property (nonatomic, weak) UISearchBar *searchBar;
@property (nonatomic, strong)SupplyAndDemandSearchSuggestionVC *searchSuggestion;
@end

@implementation SupplyAndDemandSearchVC
-(void)leftBarAction{
        [self.navigationController popViewControllerAnimated:YES];
}
-(void)rightBarAction{
    //    [self.navigationController popViewControllerAnimated:YES];
}
-(void)seachTapAction{
    
}
- (UIImage*)searchFieldBackgroundImage {
    
    UIColor*color = [UIColor whiteColor];
    
    CGFloat cornerRadius = 5;
    
    CGRect rect =CGRectMake(0,0,32,32);
    
    UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius];
    
    roundedRect.lineWidth=0;
    
    UIGraphicsBeginImageContextWithOptions(rect.size,NO,0.0f);
    
    [[UIColor grayColor] set];
    
    [color setFill];
    
    [roundedRect fill];
    
    [roundedRect stroke];
    
    [roundedRect addClip];
    
    UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建搜索框
    [[UISearchBar appearance] setSearchFieldBackgroundImage:[self searchFieldBackgroundImage] forState:UIControlStateNormal];
    UIImageView *titleView = [[UIImageView alloc] initWithFrame:CGRectZero];
    titleView.backgroundColor=[UIColor whiteColor];
    titleView.layer.cornerRadius = 5;
    titleView.userInteractionEnabled=YES;
    titleView.image=[UIImage imageNamed:@"搜索背景"];
    titleView.layer.masksToBounds=YES;
    self.navigationItem.titleView = titleView;
    
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:titleView.bounds];
    searchBar.placeholder = @"搜索内容";
    searchBar.backgroundImage = [UIImage imageNamed:@"clearImage"];
    searchBar.delegate = self;
    [titleView addSubview:searchBar];
    [searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.offset(30);
        make.centerY.offset(0);
        make.width.offset(SCREEN_WIDTH-100);
    }];
    searchBar.tintColor=[UIColor blueColor];
    self.searchBar = searchBar;
    [searchBar setImage:[UIImage imageNamed:@"搜索"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    UITextField * searchField = [searchBar valueForKey:@"_searchField"];
    searchField.font=[UIFont systemFontOfSize:13];
    [searchField setValue:[UIFont boldSystemFontOfSize:13] forKeyPath:@"_placeholderLabel.font"];
    
    
    
    
    UIBarButtonItem* rightBar=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:Localized(@"更多")] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarAction)];
    
    self.navigationItem.rightBarButtonItem=rightBar;
    
    UIBarButtonItem* leftBar=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:Localized(@"返回")] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarAction)];
    self.navigationItem.leftBarButtonItem=leftBar;
    
    
    [[UISearchBar appearance] setSearchFieldBackgroundImage:[self searchFieldBackgroundImage] forState:UIControlStateNormal];
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    [self.view addSubview:self.myTableview];
    [self.myTableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(0);
        make.left.right.equalTo(self.view).priorityHigh();
        make.bottom.equalTo(self.view).with.offset(0);
    }];

    self.searchSuggestion=[[SupplyAndDemandSearchSuggestionVC alloc]init];
    self.searchSuggestion.hidden = YES;
    self.searchSuggestion.backgroundColor=[UIColor redColor];
    [self.view addSubview:self.searchSuggestion];
    [self.searchSuggestion mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view);
    }];
    // Do any additional setup after loading the view.
}

#pragma mark - UISearchBarDelegate -
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
//    [self pushToSearchResultWithSearchStr:searchBar.text];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (searchBar.text == nil || [searchBar.text length] <= 0) {
        self.searchSuggestion.hidden = YES;
        //        [self.view bringSubviewToFront:_searchView];
    } else {
        self.searchSuggestion.hidden = NO;
        //        [self.view bringSubviewToFront:_searchSuggestVC.view];
        //        [_searchSuggestVC searchTestChangeWithTest:searchBar.text];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SupplyAndDemandCell *cell=[SupplyAndDemandCell theShareCellWithTableView:tableView];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    //分割线补全
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}

//MARK:-GETTER
- (UITableView *)myTableview{
    if (!_myTableview) {
        _myTableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableview.delegate = self;
        _myTableview.dataSource = self;
        _myTableview.backgroundColor=[UIColor groupTableViewBackgroundColor];
        _myTableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableview.showsVerticalScrollIndicator=NO;
        _myTableview.scrollEnabled = YES;
    }
    return _myTableview;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
